package com.house.rent.ui.activity;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.base.lib.mvp.present.BasePresenter;
import com.base.lib.ui.BaseAppActivity;
import com.base.lib.util.JSONUtils;
import com.base.lib.util.StatusBarUtil;
import com.house.rent.Constants;
import com.house.rent.R;
import com.house.rent.entity.BaseEntity;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import butterknife.ButterKnife;

public abstract class BaseActivity<P extends BasePresenter> extends BaseAppActivity<P> {

    private Map<String, Object> paramMap = new HashMap<>(16);

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        initStatusBar();
        super.onCreate(savedInstanceState);
    }

    protected void initStatusBar() {
        StatusBarUtil.setColor(this, ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary), 0);
        StatusBarUtil.setDarkMode(this);
    }

    @Override
    public void setContentView(View contentView) {
        super.setContentView(contentView);
        ButterKnife.bind(this, contentView);
    }

    public void addParams(String key, Object value) {
        paramMap.put(key, value);
    }

    public <T> void sendRequest(String api, Class<T> clsType) {
        Map<String, Object> params = new LinkedHashMap<>();
        params.put("params", paramMap);

        String paramJson = JSONUtils.getJsonString(params);
        OkGo.post(Constants.getBaseUrl() + api)
                .upJson(paramJson)
                .execute(new InnerCallBack(api, clsType));
    }

    /**
     * 网络请求成功回调
     */
    protected <T> void onRequestSuccess(String reqId, T t) {

    }

    /**
     * 网络请求失败回调
     */
    protected void onRequestFail(String reqId, String message) {
        showMessage(message);
    }


    private class InnerCallBack<T> extends StringCallback {

        private String api;
        private Class<T> clsType;

        private InnerCallBack(String api, Class<T> clsType) {
            this.api = api;
            this.clsType = clsType;
        }

        @Override
        public void onStart(Request<String, ? extends Request> request) {
            super.onStart(request);
            showLoading();
        }

        @Override
        public void onSuccess(Response<String> response) {
            BaseEntity baseEntity = JSONUtils.parseObject(response.body(), BaseEntity.class);
            if (baseEntity != null) {
                if (baseEntity.isSuccess()) {
                    if (baseEntity.getData() instanceof Map) {
                        T t = JSONUtils.parseObject(baseEntity.getData().toString(), clsType);
                        onRequestSuccess(api, t);
                    } else if (baseEntity.getData() instanceof List) {
                        List<T> list = JSONUtils.parseArray(baseEntity.getData().toString(), clsType);
                        onRequestSuccess(api, list);
                    } else {
                        onRequestSuccess(api, null);
                    }
                } else {
                    onRequestFail(api, baseEntity.getMsg());
                }
            } else {
                onRequestFail(api, "服务器错误");
            }
        }

        /**
         * 请求失败，响应错误，数据解析错误等，都会回调该方法， UI线程
         */
        @Override
        public void onError(Response<String> response) {
            super.onError(response);
            onRequestFail(api, "服务器错误");
        }

        @Override
        public void onFinish() {
            super.onFinish();
            hideLoading();
        }
    }
}

